package com.lc.luzhengtang.fragment;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.CouponListOutDateAdapter;
import com.lc.luzhengtang.bean.CouponBean;
import com.lc.luzhengtang.bean.CouponOutDataBean;
import com.lc.luzhengtang.conn.ConnCoupon;
import com.lc.luzhengtang.divider.SimpleDividerItemDecoration;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.lc.luzhengtang.R.id.card_title_tv;

/**
 * Created by Administrator on 2016/4/17.
 */
public class Fragment_OutDate extends BaseFragment3 implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(card_title_tv)
    TextView cardTitleTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    Unbinder unbinder;
    private String mTitle;

    //    private CouponListAdapter newsAdapter;
    private int currentPage = 1;

    //已过期
    CouponListOutDateAdapter couponListAdapter;
    List<CouponOutDataBean> couponoutData = new ArrayList<>();

    private View notDataView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_coupon, null);

        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);

        unbinder = ButterKnife.bind(this, v);
       notDataView = BoundViewHelper.boundView(this, ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(getActivity())
                .inflate(R.layout.empty_view, null)));
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        initData();
        return v;
    }

    @Override
    protected void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        couponListAdapter=new CouponListOutDateAdapter(null);
        couponListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        couponListAdapter.isFirstOnly(false);
        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider2);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 40));

        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 40));
        recyclerView.setAdapter(couponListAdapter);
        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);


        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void getData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        new ConnCoupon(BaseApplication.basePreferences.readUid(), new AsyCallBack<CouponBean>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
//                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CouponBean couponBean) throws Exception {
                super.onSuccess(toast, type, o, couponBean);
                List<CouponBean.DataBean> results = couponBean.getData();
                if (results != null) {
                    couponoutData.clear();
                    currentPage = 1;
                }



                for (CouponBean.DataBean people : results) {

                    if (people.getStatus() == 3) {

                        CouponOutDataBean couponNotuseBean = new CouponOutDataBean();
                        couponNotuseBean.setId(people.getId());
                        couponNotuseBean.setBigmoney(people.getBigmoney());
                        couponNotuseBean.setEnd_time(people.getEnd_time());
                        couponNotuseBean.setStart_time(people.getStart_time());
                        couponNotuseBean.setSmallmoney(people.getSmallmoney());
                        couponoutData.add(couponNotuseBean);
                    }
                    if (couponoutData.isEmpty()){
                        //未使用
                        couponListAdapter.setEmptyView(notDataView);

                    }else {
                        couponListAdapter.setNewData(couponoutData);
                    }

//                    couponListAdapter.setNewData(couponoutData);

                    //已使用
//                couponListAdapter = new CouponListOutDateAdapter(couponoutData);



                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
//                dismissLoading();

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                setRefreshing(false);
//                dismissLoading();
            }
        }


        ).execute(getActivity(), false);
    }


    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

}
